﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geodatabase;
using Akiha.Core;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataManagementTools;

namespace ArcMap_AddIn_Tools.FeatureClass.UpdateDjzqbmNew
{
    public partial class FormUpdateDjzqbm : Form
    {

        private IFeatureClass pFea_ZD = null;
        private IFeatureClass pFea_DJZQ = null;




        public FormUpdateDjzqbm()
        {
            InitializeComponent();
        }



        private void FormUpdateDjzqbm_Load(object sender, EventArgs e)
        {
            //初始化用户控件
            customDblCombo1.FirstLable = "宗地点图层";
            customDblCombo1.SecondLable = "宗地编码字段";
            customDblCombo1.Map = ArcMap.Document.FocusMap;
            customDblCombo1.RefreshControls();


            customDblCombo2.FirstLable = "地级子区图层";
            customDblCombo2.SecondLable = "地籍子区字段";
            customDblCombo2.Map = ArcMap.Document.FocusMap;
            customDblCombo2.RefreshControls();

        }




       


        





        private void button1_Click(object sender, EventArgs e)
        {



            pFea_ZD = customDblCombo1.SelFeatureClass;
            pFea_DJZQ = customDblCombo2.SelFeatureClass;
            
            if (customDblCombo2.SelField==null)
            {
                return;
            }
            string strDJZQ_SelFieldName = customDblCombo2.SelField.Name;


            IQueryFilter QueryFilter = new QueryFilterClass();
            QueryFilter.SubFields = "*";
            QueryFilter.WhereClause = string.Format("");
            List<IFeature> djzqList = FeatureClassWrapper.GetFeaturesFromFeatureClass(pFea_DJZQ, QueryFilter);

            for (int i = 0; i < djzqList.Count; i++)
            {
                IFeature djzqFeature = djzqList[i];
                IGeometry geo = djzqFeature.Shape;

                string djzq = FeatureWrapper.GetFieldValue(djzqFeature, strDJZQ_SelFieldName).ToString();

                IEnvelope env = geo.Envelope;
                //空间查询在地籍子区中的宗地，
                ISpatialFilter spatialFilter = new SpatialFilterClass();
                spatialFilter.Geometry = geo;
                spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                List<IFeature> listFeature = FeatureClassWrapper.GetFeaturesFromFeatureClass(pFea_ZD, spatialFilter);

                if (listFeature.Count == 0)
                {
                    continue;
                }

                UpdateFeature(pFea_ZD, listFeature, djzq, env, i + 1);


                string txt = string.Format("地籍子区进度：{0}/{1}", i + 1, djzqList.Count);
                this.label_DJZQ.Text = txt;

                //这样会影响效率，但是能防止假死
                Application.DoEvents();
            }



        }


        private void UpdateFeature(IFeatureClass FC, List<IFeature> listFeature, string djzq, IEnvelope env, int djzqindex)
        {
            string strZDBM_field = customDblCombo1.SelField.Name;

            List<IFeature> newListFeatue = new List<IFeature>();
            int rowindex = (int)Math.Round((env.YMax - env.YMin) / 25) + 1;
            string label = "";
            for (int i = 0; i < rowindex; i++)
            {
                double y_max = env.YMax - 25 * i;
                double y_min = env.YMax - 25 - 25 * i;
                List<IFeature> part_features = GetFeature(listFeature, y_min, y_max, i + 1);
                for (int j = 0; j < part_features.Count; j++)
                {
                    newListFeatue.Add(part_features[j]);
                    string txt = string.Format("宗地进度：{0}/{1}", newListFeatue.Count, listFeature.Count);
                    this.label_ZD.Text = txt;

                    Application.DoEvents();
                }
            }

            for (int i = 0; i < newListFeatue.Count; i++)
            {
                IFeature feature = newListFeatue[i];
                Dictionary<string, object> attribute = new Dictionary<string, object>();
                string bm = MathWrapper.FormatIntToStringAddZero((i + 1).ToString(), 5);
                string djqbm = djzq + "GB" + bm;
                attribute.Add(strZDBM_field, djqbm);
                FeatureClassWrapper.UpdateFeatureValues(FC, feature, attribute);
            }
        }


        private List<IFeature> GetFeature(List<IFeature> features, double y_min, double y_max, int rowindex)
        {
            List<IFeature> listFeature = new List<IFeature>();
            for (int i = 0; i < features.Count; i++)
            {
                IFeature pointFeature = features[i];
                IGeometry pGeo = pointFeature.Shape;
                IPoint point = ((IArea)pGeo).Centroid;

                if (point.Y <= y_max && point.Y > y_min)
                {

                    if (listFeature.Count == 0)
                    {
                        listFeature.Add(pointFeature);
                    }
                    for (int j = 0; j < listFeature.Count; j++)
                    {
                        IFeature featue = listFeature[j];
                        IGeometry pTmpGeo = featue.Shape;

                        IPoint featue_point = ((IArea)pTmpGeo).Centroid;
                        if (point.X < featue_point.X)
                        {
                            if (!listFeature.Contains(pointFeature))
                            {
                                listFeature.Insert(j, pointFeature);
                                break;
                            }
                        }
                    }
                    if (!listFeature.Contains(pointFeature))
                    {
                        listFeature.Add(pointFeature);
                    }
                }
            }

            return listFeature;
        }




    }
}
